import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
import axios from 'axios'
import echarts from 'echarts'
Vue.prototype.$echarts = echarts

Vue.config.productionTip = false

Vue.prototype.$http = axios

import moment from "moment";
Vue.prototype.moment = moment;

// 添加请求拦截器
axios.interceptors.request.use(
    config => {
      if(config.url != 'api/login'){
        config.headers.Authorization = window.sessionStorage.getItem('token');
      }
      return config;
    }
  );

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    if (response.data.flag == false && response.data.flag != undefined) {
        ElementUI.Message({
            message: response.data.message,
            type: 'error'
        });
        //拒绝方法执行
        return Promise.reject(response);
    }
    return response;
}, function (error) {
    //接口调用出现其它非200状态码
    ElementUI.Message({
        message: '服务出错,请联系管理员!',
        type: 'error'
    });
});

router.beforeEach((to, from, next) => {
    if(to.name == 'login'){
        next();
    }else{
        if(window.sessionStorage.getItem('token')){
            next();
        }else{
            next({path:"/"});
        }
    }
});

function clone(obj){
  var o, obj;
  if (obj.constructor == Object){
      o = new obj.constructor();
  }else{
      o = new obj.constructor(obj.valueOf());
  }
  for(var key in obj){
      if ( o[key] != obj[key] ){
          if ( typeof(obj[key]) == 'object' ){
              o[key] = clone(obj[key]);
          }else{
              o[key] = obj[key];
          }
      }
  }
  o.toString = obj.toString;
  o.valueOf = obj.valueOf;
  return o;
}

Vue.prototype.clone = clone

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
